package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//-------------------------------------上下分白名单---------------------------------------------

//新增白名单用户
func NewWhiteUser(cp map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChess)
	values, sql := InsertSql(TableWhiteList, cp)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

func UpdateWhiteUser(cp map[string]interface{}, uid int64, multiOrm ...orm.Ormer) error {
	o := NewOrm(multiOrm, DBChess)
	condition := fmt.Sprintf(`uid = %v`, uid)
	values, sql := UpdateSql(TableWhiteList, cp, condition)
	_, err := o.Raw(sql, values).Exec()
	return err
}

//查询用户是否已经在名单里
func WhiteUserById(uid int64, multiOrm ...orm.Ormer) (int64, error) {
	sql := fmt.Sprintf(`
		SELECT
			uid
		FROM
			%v
		WHERE
			uid = %v
		LIMIT 1
	`, TableWhiteList, uid)
	o := NewOrm(multiOrm, DBChess)
	var id int64
	err := o.Raw(sql).QueryRow(&id)
	if CheckNoExist(err) {
		return 0, nil
	}
	return id, err
}

//移出白名单用户
func DeleteWhiteUser(uid int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChess)
	condition := fmt.Sprintf("uid = %v", uid)
	sql := DeleteSql(TableWhiteList, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//白名单列表
func ListWhiteUser(p ListParams, uid int64, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	where := " 1=1 "
	if uid != 0 {
		where = fmt.Sprintf("w.uid = %v", uid)
	}
	sql := fmt.Sprintf(`
		SELECT
			w.*, u.imageurl AS head_img, u.nickname
		FROM
			%v AS w
		LEFT JOIN
			%v AS u
		ON w.uid = u.uid
		WHERE
			%v
		ORDER BY
			w.create_time DESC
		LIMIT ?,?
	`, TableWhiteList, TableClientUser, where)
	o := NewOrm(multiOrm, DBChess)

	var wu []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&wu)
	if CheckError(err) {
		return wu, 0, err
	}

	if uid != 0 {
		return wu, 1, nil
	}
	var count int64
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v`,
		TableWhiteList)
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return wu, 0, err
	}
	return wu, count, nil
}

//更新俱乐部上下分的权限
func UpdateClubContest(uid, isContest int64) (int64, error) {
	sql := fmt.Sprintf(`UPDATE %v SET is_contest = %v WHERE host_id = %v`,
		TableClub, isContest, uid)
	o := NewDBOrm(DBChess)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	_, err = Redis.HSet(KeyContestList, uid, isContest)
	if err != nil {
		LogPc.Debug(fmt.Sprintf("更新用户(ID:%v)能否上下分(1-能,0-否,结果:%v)redis缓存失败", uid, isContest))
	}

	sql = fmt.Sprintf(`UPDATE %v SET is_contest = %v WHERE uid = %v`,
		TableWhiteList, isContest, uid)
	result, err = o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//----------------------------大区游戏--------------------------
//新增大区游戏
func NewGroupGame(cp map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	values, sql := InsertSql(TableGroupGame, cp)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//查询大区游戏是否已经存在了
func GroupGameById(groupId, gameId int64, multiOrm ...orm.Ormer) (bool, error) {
	sql := fmt.Sprintf(`
		SELECT
			group_id,game_id
		FROM
			%v
		WHERE
			group_id = %v AND game_id = %v
		LIMIT 1
	`, TableGroupGame, groupId, gameId)
	o := NewOrm(multiOrm, DBChessCenter)
	var rid, aid int64
	err := o.Raw(sql).QueryRow(&rid, &aid)
	if CheckNoExist(err) {
		return false, nil
	}
	if CheckError(err) {
		return true, err
	}
	if groupId == rid && gameId == aid {
		return true, nil
	}
	return false, nil
}

//删除大区游戏
func DeleteGroupGame(groupId, gameId int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	condition := fmt.Sprintf("group_id = %v AND game_id = %v", groupId, gameId)
	sql := DeleteSql(TableGroupGame, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//列出大区游戏
func ListGroupGame(p ListParams, groupId int64, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	where := fmt.Sprintf("group_id = %v", groupId)
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			%v
		LIMIT ?,?
	`, TableGroupGame, where)
	o := NewOrm(multiOrm, DBChessCenter)

	var wu []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&wu)
	if CheckError(err) {
		return wu, 0, err
	}

	var count int64
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE %v`, TableGroupGame, where)
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return wu, 0, err
	}
	return wu, count, nil
}
